package com.leancloud.login.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.leancloud.login.LoginPath;
import com.leancloud.login.R;
import com.vise.xsnow.event.BusManager;

import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.event.LoginChangeEvent;
import tech.com.commoncore.utils.DataUtils;
import tech.com.commoncore.utils.RegUtils;
import tech.com.commoncore.utils.ToastUtil;

@Route(path = LoginPath.LOGIN_PHONE)
public class SmsLoginActivity extends BaseTitleActivity implements View.OnClickListener {

    private EditText phoneEt, verifyEt;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTextColor(Color.WHITE)
                .setLeftTextDrawable(null)
                .setBgColor(Color.TRANSPARENT)
                .setDividerHeight(0)
                .setStatusBarLightMode(true)
                .setRightTextDrawable(R.drawable.login_icon_back)
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_phone_sms_login;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        phoneEt = findViewById(R.id.login_input_phone_et);
        verifyEt = findViewById(R.id.login_input_verify_et);


        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.tv_registered).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_login) {
            doLogin();
        } else if (v.getId() == R.id.tv_registered) {
//            FastUtil.startActivity(this, RegisterPhoneActivity.class);
            finish();
        }
    }

    private boolean verifyNumber() {
        String number = phoneEt.getText().toString().trim();
        if (number == null || number.isEmpty()) {
            ToastUtil.show("请输入手机号");
            return false;
        }
        if (!RegUtils.isMobile(number)) {
            ToastUtil.show("请输入正确的号码");
            return false;
        }
        return true;
    }

    private void requestLoginSms() {
        String number = phoneEt.getText().toString();
        AVUser.requestLoginSmsCodeInBackground(number, new RequestMobileCodeCallback() {
            @Override
            public void done(AVException e) {
                if (e != null) {
                    ToastUtil.show("短信发送失败,可能太过频繁");
                }
            }
        });
    }

    private void doLogin() {
        if (DataUtils.isEmpty(phoneEt.getText())) {
            ToastUtil.show("请输入账号");
            return;
        }

        if (DataUtils.isEmpty(verifyEt.getText())) {
            ToastUtil.show("请输入验证码");
            return;
        }

        final String number = phoneEt.getText().toString().trim();
        final String smsCode = verifyEt.getText().toString().trim();

        AVUser.signUpOrLoginByMobilePhoneInBackground(number, smsCode, new LogInCallback<AVUser>() {
            @Override
            public void done(AVUser avUser, AVException e) {
                if (e == null) {
                    BusManager.getBus().post(new LoginChangeEvent(true)); //通知登录改变
                    SmsLoginActivity.this.finish();
                } else {
                    ToastUtil.show("手机号或密码错误");
                }
            }
        });
    }

}
